<?php
require_once 'TVB/pms/pdes/common/util/Unpublished.class.php';
require_once 'TVB/pms/pdes/common/util/ChangeLogTools.class.php';

class CastSwitchAction extends BaseAction 
{
	public $id;

	public $ut;

	public function execute(){
		if (count($this->_errors) > 0)
		{
			return "filter-error";
		}

		if (isset($_REQUEST['delete']))
		{
			return $this->delete_cast();
		}
		elseif (isset($_REQUEST['clone']))
		{
			//return $this->clone_main();
		}
		elseif (isset($_REQUEST['recover']))
		{
			return	$this->recover_cast();
		}
		elseif (isset($_REQUEST['edit']))
		{
			return $this->edit_cast();
		}
		elseif (isset($_REQUEST['unpublish']))
		{
			return $this->unpublish_cast();
		}
        elseif (isset($_REQUEST['unpublish_premium']))
		{
			return $this->unpublish_premium_cast();
		}
	}    

	/**
	 * clone main 
	 */
	private function clone_main()
	{
		$clone_id = $this->clone_cast();
		$this->clone_cast_language($clone_id);

		$this->setData('type', 'cast');
		$this->setData('id', $clone_id);
		$this->setData('act', 'edit');
		$this->setData('step', 2);
		return 'clone';
	}

	/**
	 * clone cast
	 */
	private function clone_cast()
	{
		$cast = new cast();
		$clone_cast = $cast->getDetails($this->id);
		$cast->set($clone_cast);
		$cast->created_at = date("Y-m-d H:i:s", $_SERVER['REQUEST_TIME']);
		$cast->created_by = CommonUtils::get_user();
		$cast->folders = CommonUtils::init_folder("cast", $this);
		$cast->available_tag_types = CommonUtils::get_allow_tags("cast");
		$cast->status = AC::STATUS_NEW;

		$clone_id = Model::$dbo['w']->save($cast);
		if ($clone_id > 0)
		{
			$this->addInfo("cast:".AC::LOG_OP_CREATE.":".$clone_id, "add cast success", json_encode($cast));
			return $clone_id;
		}
		else
		{
			$this->addError("cast:".AC::LOG_OP_CREATE.":".$clone_id, "add cast error", json_encode($cast));
			return false;
		}
	}

	/**
	 * cloen cast language
	 */
	private function clone_cast_language($clone_id)
	{
		$cast_language = new cast_language();
		$clone_cast_language = $cast_language->getCastByID($this->id);
		$cast_language->set($clone_cast_language);
		$cast_language->cast_id = $clone_id;
		$ret = Model::$dbo['w']->save($cast_language);
		if ($ret >= 0)
		{
			$this->addInfo("cast-language:".AC::LOG_OP_CREATE.":".$clone_id, "add cast language success", json_encode($cast_language));
			return true;
		}
		else
		{
			$this->addError("cast-language:".AC::LOG_OP_CREATE.":".$clone_id, "add cast language error", json_encode($cast_language));
			return false;
		}
	}

	/**
	 * delete cast
	 */
	private function delete_cast()
	{
		try
		{
			$cast = new cast();
			$cast = $cast->get($this->id);
			$cast->status = AC::STATUS_DELETED;
			$cast->modified_at = date("Y-m-d H:i:s");
			$cast->modified_by = CommonUtils::get_user();
			$update_id = Model::$dbo['w']->save($cast);
			if ($update_id > 0)
			{
				change_log::remove(array('type'=>'cast','keys'=>array('id'=>$this->id)));
				change_log::remove(array('type'=>'cast-language','keys.cast_id'=>$this->id));
				foreach(explode(',',$cast->folders) as $fid){
					if(empty($fid)) continue;
					change_log::remove(array('type'=>'folder','keys'=>array('id'=>$fid)));
					change_log::remove(array('type'=>'folder-language','keys.folder_id'=>$fid));
					//remove change_log of image_folder-config when delete cast.  /*modified by Albie Lu on 2011-10-10*/ 
					change_log::remove(array('type'=>'image_folder-config','keys.folder_id'=>$fid));
				}
				$this->addInfo("cast:".AC::LOG_OP_DELETE.":".$this->id, "cast delete success", json_encode($cast));
			}
			else
			{
				$this->addError("cast:".AC::LOG_OP_DELETE.":".$this->id, "cast delete error", json_encode($cast));
			}

			$this->setData('type', 'cast');
			return 'delete';
		}
		catch(Exception $e)
		{
			error_log("delete cast: {$this->id}" . var_export($e,1));
	
			return 'system-error';
		}
	}

	/**
	 * edit cast
	 */
	private function edit_cast()
	{
		try
		{
			$cast = new cast();
			$cast = $cast->get($this->id);
			if ($cast->status == AC::STATUS_DELETED)
			{
				$this->setData('step', '2');
				$this->setData('act', 'view');
				$this->setData('id', $this->id);
				$this->setData('error_code', '0108');
				return 'logic-error';
			}

			$this->setData('id', $this->id);
			$this->setData('type', 'cast');
			$this->setData('step', 2);
			$this->setData('act', 'edit');

			return 'edit';
		}
		catch(Exception $e)
		{
			error_log("edit cast: {$this->id}" . var_export($e,1));
	
			return 'system-error';
		}
	}

	/**
	 * recover cast
	 */
	private function recover_cast()
	{
		try{
			$cast = new cast();
			$cast = $cast->get($this->id);
			$cast->status = AC::STATUS_NEW;
			$cast->modified_at = date("Y-m-d H:i:s");
			$cast->modified_by = CommonUtils::get_user();
			$update_id = Model::$dbo['w']->save($cast);
			if ($update_id > 0)
			{
				# change_log
				$languages  = array(language::getLanguageID(AC::LANGUAGE_ZH), language::getLanguageID(AC::LANGUAGE_EN));
				$change_log = array();
				$change_data = ChangeLogTools::getEpisodeIdsByCastId($this->id);
				$change_data['is_active']  = 1;
				$change_data['is_publish'] = 0;
				$change_data['type'] = 'cast';
				$change_data['keys'] = array('id'=>$this->id);
				$change_log[] = $change_data;
				foreach($languages as $l){
					$change_data['type'] = 'cast-language';
					$change_data['keys'] = array('cast_id'=>$this->id,'language_id'=>$l);
					$change_log[] = $change_data;
				}
				foreach(explode(',',$cast->folders) as $fid){
					if(empty($fid)) continue;
					$change_data['type'] = 'folder';
					$change_data['keys'] = array('id'=>$fid);
					$change_log[] = $change_data;

					foreach($languages as $l){
						$change_data['type'] = 'folder-language';
						$change_data['keys'] = array('folder_id'=>$fid,'language_id'=>$l);
						$change_log[] = $change_data;
					}
					
					//add change_log of image_folder-config     /*modified by Albie Lu on 2011-10-10*/ 
					$change_data['type'] = 'image_folder-config';
					$change_data['keys'] = array('folder_id'=>$fid);
					$change_log[] = $change_data;

					/** Add image & image-language to change_log for bug:7571 */
					foreach(folder_item::getItemByFolderId($fid) as $image_id){
						$change_data['type'] = 'folder_item';
						$change_data['keys'] = array('folder_id'=>$fid,'item_id'=>$image_id);
						$change_log[] = $change_data;
						$change_data['type'] = 'image';
						$change_data['keys'] = array('id'=>$image_id);
						$change_log[] = $change_data;
						foreach($languages as $l){
							$change_data['type'] = 'image-language';
							$change_data['keys'] = array('image_id'=>$image_id,'language_id'=>$l);
							$change_log[] = $change_data;
							$change_data['type'] = 'folder_item-language';
							$change_data['keys'] = array('folder_id'=>$fid,'item_id'=>$image_id,'language_id'=>$l);
							$change_log[] = $change_data;
						}
					}
				}
				array_walk($change_log,'change_log::updateOne');


				$this->addInfo("cast:".AC::LOG_OP_RECOVER.":".$this->id, "cast recover success", json_encode($cast));
			}
			else
			{
				$this->addError("cast:".AC::LOG_OP_RECOVER.":".$this->id, "cast recover error", json_encode($cast));
			}

			$this->setData('type', 'cast');
			return 'recover';
		}
		catch(Exception $e)
		{
			error_log("recover cast: " . var_export($e,1));

			return 'system-error';
		}
	}

	///** Merge to unpublish_cast
    private function unpublish_premium_cast()
    {
		Model::$dbo['w']->beginTransaction();
		try{
			$cast = new cast();
			$cast = $cast->get($this->id);
			$log_id = CommonUtils::save_log('cast', $this->id, AC::LOG_OP_UNPUBLISHING, json_encode($cast));
			$unpublish_flag = P_Unpublished::send('cast', $this->id, $cast->programme_id, $log_id);

			Model::$dbo['w']->commit();

			$this->setData('type', 'cast');
			return 'unpublish';
		}
		catch(Exception $e)
		{
			error_log("Unpublish Cast Fails: $this->id");
			error_log("cast: " . var_export($e,1));

			Model::$dbo['w']->rollBack();

			return 'system-error';
		}
    }
	// */

	/**
	 * unpublish cast
	 */
	private function unpublish_cast()
	{
		Model::$dbo['w']->beginTransaction();
		try{
			$cast = new cast();
			$cast = $cast->get($this->id);
			$log_id = CommonUtils::save_log('cast', $this->id, AC::LOG_OP_UNPUBLISHING, json_encode($cast));
			Unpublished::send('cast', $this->id, $cast->programme_id, $log_id);

			// donald 2011-03-21
			//if(CommonUtils::checkLastPublishStatus('cast',$this->id,AC::STATUS_PUBLISH_PREMIUM)){
			//	#$log_id = CommonUtils::save_log('cast', $this->id, AC::LOG_OP_PREMIUM_UNPUBLISHING, json_encode($cast));
			//	P_Unpublished::send('cast', $this->id, $cast->programme_id, $log_id);
			//}

			Model::$dbo['w']->commit();

			$this->setData('type', 'cast');
			return 'unpublish';
		}
		catch(Exception $e)
		{
			error_log("Unpublish Cast Fails: $this->id");
			error_log("cast: " . var_export($e,1));

			Model::$dbo['w']->rollBack();

			return 'system-error';
		}
	}
}
?>


